package com.cari.oa.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.cari.oa.domain.SysDictGroupEntity;
import com.cari.oa.domain.SysDictItemEntity;
import com.cari.oa.service.SysDictGroupService;
import com.cari.oa.service.SysDictItemService;
import com.cari.oa.util.Contants;
import com.cari.oa.util.Utils;

/**
 * 系统字典项controller
 * User: yexuhui
 * Date: 2012-12-27
 * Time: 下午7:04:52
 */
@Controller
@SessionAttributes("username")
public class SysDictItemController {

	@Resource
	private SysDictItemService sysDictItemService;
	@Resource
	private SysDictGroupService sysDictGroupService;
	
	public static final String MODULE_NAME = "SYS_DICTITEM_MANAGE";
	
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }
	
    @RequestMapping("/system/dictitem/list.do")
    public String list(String name, String value, String groupName, Integer status, Integer curPage, ModelMap modelMap) {
        if(curPage == null) curPage = 1;
        long totalRecords = sysDictItemService.getCount(name, value, groupName, status);
        long totalPage = totalRecords / Contants.PAGE_SIZE + (totalRecords % Contants.PAGE_SIZE != 0 ? 1 : 0);
        
        List<SysDictItemEntity> itemList = sysDictItemService.getPage(name, value, groupName, status, curPage, Contants.PAGE_SIZE);
        List<SysDictGroupEntity> groupList = sysDictGroupService.getAll();

        Map<String, String> gNameMap = new HashMap<String, String>();
        for (SysDictGroupEntity sysDictGroupEntity : groupList) {
        	gNameMap.put(sysDictGroupEntity.getEnname(), sysDictGroupEntity.getAlias());
		}

        modelMap.addAttribute("itemList", itemList);
        modelMap.addAttribute("groupList", groupList);
        modelMap.addAttribute("gNameMap", gNameMap);
        modelMap.addAttribute("curPage", curPage);
        modelMap.addAttribute("totalPage", totalPage);
        modelMap.addAttribute("totalRecords", totalRecords);

        modelMap.addAttribute("name", name);
        modelMap.addAttribute("groupName", groupName);
        modelMap.addAttribute("value", value);
        modelMap.addAttribute("status", status);

        return "system/dictitem/list";
    }
    
    @RequestMapping(value = "/system/dictitem/insert.do", method = RequestMethod.GET)
    public String enterInsert(String qs, ModelMap modelMap) {
    	List<SysDictGroupEntity> groupList = sysDictGroupService.getAll();
        modelMap.addAttribute("groupList", groupList);
        modelMap.addAttribute("qs", qs);
        return "system/dictitem/insert";
    }

    @RequestMapping(value = "/system/dictitem/view.do", method = RequestMethod.GET)
    public String enterView(String id, ModelMap modelMap) {
        SysDictItemEntity item = sysDictItemService.getById(id);
        SysDictGroupEntity groupEntity = sysDictGroupService.getByEnname(item.getGroupName());
        modelMap.addAttribute("groupName", groupEntity.getAlias());
        modelMap.addAttribute("item", item);
        return "system/dictitem/view";
    }

    @RequestMapping(value = "/system/dictitem/edit.do", method = RequestMethod.GET)
    public String enterEdit(String id, String qs, ModelMap modelMap) {
    	SysDictItemEntity item = sysDictItemService.getById(id);
    	List<SysDictGroupEntity> groupList = sysDictGroupService.getAll();
        modelMap.addAttribute("groupList", groupList);
        modelMap.addAttribute("item", item);
        modelMap.addAttribute("qs", qs);
        return "system/dictitem/edit";
    }
    
    @RequestMapping(value = "/system/dictitem/insert.do", method = RequestMethod.POST)
    @ResponseBody
    public String postInsert(@ModelAttribute("username") String username, 
    		@Valid SysDictItemEntity sysDictItemEntity, BindingResult result) {
        if(result.hasFieldErrors())
            return Utils.errorJsonResp(result.getFieldError().getDefaultMessage());

        if (sysDictItemService.existByEnname(sysDictItemEntity.getName()))
            return Utils.errorJsonResp("系统模块英文名已存在");

        sysDictItemService.insert(sysDictItemEntity);
        
        SysOperLogController.addOperLog(username, MODULE_NAME, "新增字典项" + sysDictItemEntity.getName());
        return Utils.normalJsonResp();
    }

    @RequestMapping(value = "/system/dictitem/edit.do", method = RequestMethod.POST)
    @ResponseBody
    public String postEdit(@ModelAttribute("username") String username, 
    		@Valid SysDictItemEntity sysDictItemEntity, BindingResult result) {
        if(result.hasFieldErrors())
            return Utils.errorJsonResp(result.getFieldError().getDefaultMessage());

        sysDictItemService.update(sysDictItemEntity);
        
        SysOperLogController.addOperLog(username, MODULE_NAME, "修改字典项" + sysDictItemEntity.getName());
        return Utils.normalJsonResp();
    }
    
    @RequestMapping(value = "/system/dictitem/delete.do")
    @ResponseBody
    public String postDelete(@ModelAttribute("username") String username, String id) {
    	SysDictItemEntity sysDictItemEntity = sysDictItemService.getById(id);
        if(sysDictItemEntity == null) return Utils.errorJsonResp("该字典项已不存在，请刷新页面");

        sysDictItemService.deactivate(id);
        
        SysOperLogController.addOperLog(username, MODULE_NAME, "删除字典项" + sysDictItemEntity.getName());
        return Utils.normalJsonResp();
    }
}
